﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DataModels.Common;
using GenericTools;
using GenericTools.Models;

namespace ReportingManagement.Components
{
    public partial class OtherFacilityBasicDetails : Form
    {
        private UserInformation user;
        private DataTable PROMSSettings;
        private DataTable DHIS2Settings;

        public OtherFacilityBasicDetails(UserInformation user)
        {
            InitializeComponent();
            this.user = user;            
            
            DataTable dataTable = Utilities.GetAllFacilitiesStatus();
            lstFacilities.DataSource = dataTable;
            lstFacilities.DisplayMember = "Facility Name";
        }

        private void FillOtherSystemDetails()
        {
            if (this.PROMSSettings != null)
            {
                if (this.PROMSSettings.Rows.Count == 1)
                {
                    DataRow row = this.PROMSSettings.Rows[0];
                    string facilityName = row["FacilityName"].ToString();
                    string facilityUniqueIdentifier = row["FacilityCode"].ToString();
                    string facilityDistrict = row["DistrictName"].ToString();
                    string facilityRegion = row["RegionName"].ToString();

                    txtPROMISRegion.Text = facilityRegion;
                    txtPROMISDistrict.Text = facilityDistrict;
                    txtPROMISFacilityName.Text = facilityName;
                    txtPROMISFacilityUniqueIdentifier.Text = facilityUniqueIdentifier;
                }
            }

            if (this.DHIS2Settings != null)
            {
                if (this.DHIS2Settings.Rows.Count == 1)
                {
                    DataRow row = this.DHIS2Settings.Rows[0];
                    string facilityName = row["FacilityName"].ToString();
                    string facilityUniqueIdentifier = row["FacilityCode"].ToString();
                    string facilityDistrict = row["DistrictName"].ToString();
                    string facilityRegion = row["RegionName"].ToString();

                    txtDHIS2Region.Text = facilityRegion;
                    txtDHIS2District.Text = facilityDistrict;
                    txtDHIS2FacilityName.Text = facilityName;
                    txtDHIS2FacilityUniqueIdentifier.Text = facilityUniqueIdentifier;
                }
            }
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            lstFacilities.Enabled = false;
            btnSave.Enabled = false;
            string resetButtonText = btnReset.Text;
            btnReset.Enabled = false;
            btnReset.Text = "Please wait...";
            
            Application.DoEvents();
            this.Cursor = Cursors.WaitCursor;
            lstFacilities_SelectedIndexChanged(null, null);

            lstFacilities.Enabled = true;
            btnReset.Enabled = true;
            btnReset.Text = resetButtonText;
            btnSave.Enabled = true;
            this.Cursor = Cursors.Default;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            bool okToContinue = ValidateUserInputs();
            if (okToContinue)
            {

                string internalFacilityCode = lblFacilityCode.Text;
                
                string PROMISFacilityCode = txtPROMISFacilityUniqueIdentifier.Text;
                string PROMISFacilityName = txtPROMISFacilityName.Text;
                string PROMISDistrictName = txtPROMISDistrict.Text;
                string PROMISRegionName = txtPROMISRegion.Text;

                string DHIS2FacilityCode = txtDHIS2FacilityUniqueIdentifier.Text;
                string DHIS2FacilityName = txtDHIS2FacilityName.Text;
                string DHIS2DistrictName = txtDHIS2District.Text;
                string DHIS2RegionName = txtDHIS2Region.Text;

                lstFacilities.Enabled = false;
                btnSave.Enabled = false;
                string saveButtonText = btnSave.Text;
                btnReset.Enabled = false;
                btnSave.Text = "Please wait...";
                Application.DoEvents();
                this.Cursor = Cursors.WaitCursor;

                okToContinue = false;
                do
                {
                    try
                    {
                        okToContinue = OtherSystem.SaveOtherSystemDetails(_PROMIS.Id, internalFacilityCode,
                            PROMISFacilityCode, PROMISFacilityName, PROMISDistrictName, PROMISRegionName, this.user.ID);

                        okToContinue = OtherSystem.SaveOtherSystemDetails(_DHIS2.Id, internalFacilityCode,
                            DHIS2FacilityCode, DHIS2FacilityName, DHIS2DistrictName, DHIS2RegionName, this.user.ID);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(this, ex.Message, AppConfiguration.ApplicationName,
                            MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        if (!okToContinue)
                        {
                            var response = MessageBox.Show(this, "An error occured while saving!",
                                AppConfiguration.ApplicationName, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
                            if (response == System.Windows.Forms.DialogResult.Cancel) okToContinue = true;
                        }
                    }
                } while (!okToContinue);

                lstFacilities.Enabled = true;
                btnReset.Enabled = true;
                btnSave.Text = saveButtonText;
                btnSave.Enabled = true;
                this.Cursor = Cursors.Default;
            }
        }

        private bool ValidateUserInputs()
        {
            if (string.IsNullOrEmpty(txtPROMISRegion.Text))
            {
                MessageBox.Show(this, "Region Name is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPROMISRegion.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtPROMISDistrict.Text))
            {
                MessageBox.Show(this, "District Name is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPROMISDistrict.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtPROMISFacilityName.Text))
            {
                MessageBox.Show(this, "Facility Name is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPROMISFacilityName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtPROMISFacilityUniqueIdentifier.Text))
            {
                MessageBox.Show(this, "Facility Unique Identifier is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtPROMISFacilityUniqueIdentifier.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtDHIS2Region.Text))
            {
                MessageBox.Show(this, "Region Name is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDHIS2Region.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtDHIS2District.Text))
            {
                MessageBox.Show(this, "District Name is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDHIS2District.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtDHIS2FacilityName.Text))
            {
                MessageBox.Show(this, "Facility Name is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDHIS2FacilityName.Focus();
                return false;
            }

            if (string.IsNullOrEmpty(txtDHIS2FacilityUniqueIdentifier.Text))
            {
                MessageBox.Show(this, "Facility Unique Identifier is required!", AppConfiguration.ApplicationName,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtDHIS2FacilityUniqueIdentifier.Focus();
                return false;
            }

            return true;
        }

        private static List<OtherSystem> _KnownSystems;
        private static OtherSystem _PROMIS;
        private static OtherSystem _DHIS2;
        static OtherFacilityBasicDetails()
        {
            _PROMIS = OtherSystem.GetOtherSystem(OtherKnownSystem.PROMIS);
            _DHIS2 = OtherSystem.GetOtherSystem(OtherKnownSystem.DHIS2);

            _KnownSystems = new List<OtherSystem>();
            _KnownSystems.Add(_PROMIS);
            _KnownSystems.Add(_DHIS2);
        }

        private void lstFacilities_SelectedIndexChanged(object sender, EventArgs e)
        {
            ClearUserInputs();
            DataRowView row = lstFacilities.SelectedItem as DataRowView;
            if (row != null)
            {
                string facilityCode = row["Facility Code"].ToString();
                lblFacilityCode.Text = facilityCode;
                lblFacilityName.Text = row["Facility Name"].ToString();
                lblFacilityDistrict.Text = row["District Name"].ToString();
                lblFacilityRegion.Text = row["Region Name"].ToString();

                this.PROMSSettings = Utilities.GetOtherSystemsFacilityDetails(_PROMIS.Id, facilityCode, user.ID);
                this.DHIS2Settings = Utilities.GetOtherSystemsFacilityDetails(_DHIS2.Id, facilityCode, user.ID);

                FillOtherSystemDetails();

                if (!_PROMIS.ProvidesFacilityUniqueIdentifier)
                {
                    txtPROMISFacilityUniqueIdentifier.ReadOnly = true;
                    if (string.IsNullOrEmpty(txtPROMISFacilityUniqueIdentifier.Text))
                    {
                        txtPROMISFacilityUniqueIdentifier.Text = facilityCode;
                        //txtFacilityName.Text = this.facility.Name;
                    }
                }

                if (!_DHIS2.ProvidesFacilityUniqueIdentifier)
                {
                    txtDHIS2FacilityUniqueIdentifier.ReadOnly = true;
                    if (string.IsNullOrEmpty(txtDHIS2FacilityUniqueIdentifier.Text))
                    {
                        txtPROMISFacilityUniqueIdentifier.Text = facilityCode;
                        //txtFacilityName.Text = this.facility.Name;
                    }
                }
            }
        }

        private void ClearUserInputs()
        {
            lblFacilityCode.Text = string.Empty;
            lblFacilityRegion.Text = string.Empty;
            lblFacilityDistrict.Text = string.Empty;
            lblFacilityName.Text = string.Empty;

            txtPROMISRegion.Clear();
            txtPROMISDistrict.Clear();
            txtPROMISFacilityName.Clear();
            txtPROMISFacilityUniqueIdentifier.Clear();

            txtDHIS2Region.Clear();
            txtDHIS2District.Clear();
            txtDHIS2FacilityName.Clear();
            txtDHIS2FacilityUniqueIdentifier.Clear();

            Application.DoEvents();
        }
    }
}
